﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;

namespace XtremBiking.Web.Atributes
{
    //public static class SessionManager : ISessionManager
    //{
    //    public static void RegisterSession(string key, object obj)
    //    {
    //        System.Web.HttpContext.Current.Session[key] = obj;
    //    }

    //    public static void FreeSession(string key)
    //    {
    //        System.Web.HttpContext.Current.Session[key] = null;
    //    }


    //    public static bool CheckSession(string key)
    //    {
    //        if (System.Web.HttpContext.Current.Session[key] != null)
    //            return true;
    //        else
    //            return false;
    //    }


    //    public static object ReturnSessionObject(string key)
    //    {
    //        if (CheckSession(key))
    //            return System.Web.HttpContext.Current.Session[key];
    //        else
    //            return null;
    //    }
    //}




}


//public interface ISessionManager : IDisposable
//{
//    ISession GetSession();

//}


[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MyAuthorizationAttribute : AuthorizeAttribute
{
    public XtremBiking.Logica.Enumerados.Usuarios.Perfil AccessLevel { get; set; }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null)
        {
            throw new ArgumentNullException("httpContext");
        }

        IPrincipal user = httpContext.User;

        String[] values = user.Identity.Name.Split(new Char[] { '#' });

        string displayProfile = values[3].ToString();

        if (!user.Identity.IsAuthenticated)
        {
            return false;
        }

        if (AccessLevel.Equals(XtremBiking.Logica.Enumerados.Usuarios.Perfil.Usuario) && displayProfile.Equals(XtremBiking.Logica.Enumerados.Usuarios.Perfil.Usuario.ToString())) 
        {
            return true;

        }
        else if (AccessLevel.Equals(XtremBiking.Logica.Enumerados.Usuarios.Perfil.Usuario) && displayProfile.Equals(XtremBiking.Logica.Enumerados.Usuarios.Perfil.Administrador.ToString())) 
        {
            return true;
        }
        else if (AccessLevel.Equals(XtremBiking.Logica.Enumerados.Usuarios.Perfil.Administrador) && displayProfile.Equals(XtremBiking.Logica.Enumerados.Usuarios.Perfil.Administrador.ToString()))
        {
            return true;
        }
        else 
        {
            return false;
        }

    }
}